/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.yongbao.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.TreeUtil;
import com.google.common.collect.Lists;
import com.yongbao.common.domain.BaseTreeVO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhanghong
 * @version TreeUtil.java, v 0.1 2022/11/9 17:34 下午 TreeUtil Exp $$
 * https://www.jianshu.com/p/0808c0029b2d
 */
public class TreeUtils extends TreeUtil {

    /**
     * list转tree
     *
     * @param treeList
     * @param rootNodePid
     * @return
     */
    public static <T> List<? extends BaseTreeVO> streamToTree(List<? extends BaseTreeVO> treeList, Long rootNodePid) {
        if (CollectionUtils.isEmpty(treeList)) {
            return null;
        }
        List<? extends BaseTreeVO> list = treeList.stream()
                // 过滤父节点
                .filter(parent -> parent.getParentId().equals(rootNodePid))
                // 把父节点children递归赋值成为子节点
                .map(child -> {
                    child.setChildren(streamToTree(treeList, child.getId()));
                    return child;
                }).collect(Collectors.toList());
        return list;
    }

    public static List<? extends BaseTreeVO> streamToTreeChild(List<? extends BaseTreeVO> treeList, Long rootNodePid) {
        List<? extends BaseTreeVO> list = treeList.stream()
                // 过滤父节点
                .filter(parent -> parent.getParentId().equals(rootNodePid))
                // 把父节点children递归赋值成为子节点
                .map(child -> {
                    List<? extends BaseTreeVO> children = streamToTreeChild(treeList, child.getId());
                    if (CollectionUtils.isNotEmpty(children)) {
                        child.setChildren(children);
                    }
                    return child;
                }).collect(Collectors.toList());
        return list;
    }

    /**
     * list转tree
     *
     * @param treeList
     * @param rootNodePid
     * @return
     */
    public static List<? extends BaseTreeVO> streamToTreeByParentId(List<? extends BaseTreeVO> treeList, Long rootNodePid) {
        List<? extends BaseTreeVO> list = treeList.stream()
                // 过滤父节点
                .filter(parent -> parent.getParentId().equals(rootNodePid))
                // 把父节点children递归赋值成为子节点
                .map(child -> {
                    List<? extends BaseTreeVO> baseTreeVOS = streamToTreeByParentId(treeList, child.getId());
                    if (CollectionUtil.isEmpty(baseTreeVOS)){
                        child.setChildren(null);
                    }else{
                        child.setChildren(baseTreeVOS);
                    }
                    return child;
                }).collect(Collectors.toList());
        return list;
    }

    /**
     * list转tree
     *
     * @param treeList
     * @param rootNodePid
     * @return
     */
    public static List<? extends BaseTreeVO> streamToTreeAndOrder(List<? extends BaseTreeVO> treeList, Long rootNodePid) {
        List<? extends BaseTreeVO> list = treeList.stream()
                // 过滤父节点
                .filter(parent -> parent.getParentId().equals(rootNodePid))
                // 把父节点children递归赋值成为子节点
                .map(child -> {
                    List<? extends BaseTreeVO> baseTreeVOS = streamToTreeAndOrder(treeList, child.getId());
                    if (CollectionUtils.isNotEmpty(baseTreeVOS)) {
                        child.setChildren(baseTreeVOS);
                    }else {
                        child.setChildren(Lists.newArrayList());
                    }
                    return child;
                }).collect(Collectors.toList());
        return list;
    }
}
